'''
Created on 26-11-2012

@author: LONG HOANG GIANG
'''
from CrawlerLib2 import commonlib
from Model.storymodelv2 import StoryModel
import re
import os
import datetime
import glob

def process():
    
    index_file = PBGS_FOLDER + "index/index.txt"
    arr = glob.glob1(PBGS_FOLDER, "*.txt")
    database = StoryModel(DATABASE)
    database.open(True)
    idxStr = commonlib.file_get_contents(index_file).strip()
    chapters = idxStr.split("\n")
    for i in range(len(chapters)):
        chapter = chapters[i]
        chapter = commonlib.toUpper(chapter)
        filePath = "{0}{1}".format(PBGS_FOLDER, arr[i])
        content = commonlib.file_get_contents(filePath).strip()
        title = content[:content.find("\n")].strip()
        if title == '' or len(title)>100: title = chapter
        content = re.sub(r"({0})".format(title), "<b>\\1</b><br /><hr /><br />", content)
        content = content.replace("\n", "<br />")
        print '___________________________________'
        print chapter
        print '___________________________________'
        print content, "\n\n"
        database.add_story(chapter, content, 0)
    database.close()
    
if __name__ == '__main__':
    
    
    DATABASE = "/longhoanggiang/database/{0}".format("camonedaya")
    PBGS_FOLDER = "/longhoanggiang/pbgs/"
    if not os.path.exists(PBGS_FOLDER): os.makedirs(PBGS_FOLDER, 0777)
    
    process()
    
    print '> Finished at {0}'.format(datetime.datetime.now())
    os._exit(1)